﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Tomato.Cryptography
{
    class XorEncryptor : ICryptoTransform
    {
        public byte[] Key { get; set; }

        public bool CanReuseTransform
        {
            get { return true; }
        }

        public bool CanTransformMultipleBlocks
        {
            get { return true; }
        }

        public int InputBlockSize
        {
            get { return Key.Length; }
        }

        public int OutputBlockSize
        {
            get { return Key.Length; }
        }

        public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
        {
            for (int i = 0; i <= inputCount - 1; i++)
            {
                outputBuffer[outputOffset + i] = (byte)(inputBuffer[inputOffset + i] ^ Key[i % Key.Length]);
            }

            return inputCount;
        }

        public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
        {
            byte[] bytes = new byte[inputCount];

            for (int i = 0; i <= inputCount - 1; i++)
            {
                bytes[i] = (byte)(inputBuffer[inputOffset + i] ^ Key[i % Key.Length]);
            }

            return bytes;
        }

        public void Dispose()
        {
        }
    }
}
